package com.mt.backup;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;

import com.herohead.Misc;

import redis.clients.jedis.Jedis;

public class sendStats {

	static Jedis jedis;
	static Misc m;

	static String runningID; 
	static String tempKeys = "prod:backup-temp:keys#";
	static String runningKey = "prod:backup-stats:nw:eu:running:";
	static String sgBodyKey = "prod:backup-stats:nw:eu:sgBody#";

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileInput = args[0];
		String saveGroupLog = null;

		File file = new File(fileInput);

		try {

			saveGroupLog = FileUtils.readFileToString(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		List<String> sgList = new ArrayList<String>();
		
		sgList = returnList(saveGroupLog);
		

		/**

		String[] tempList = saveGroupLog.split("\n");

		for (String s : tempList) {
			sgList.add(s);
		}

**/
		
		//Long ll = convertToBytes("5 KB");
		//ll = ll /1024/1024;
		//System.out.println(ll);
		//System.exit(0);

		String redisHost = "172.18.38.14";
		//String redisHost = "rp-1";
		jedis = new Jedis(redisHost); 

		runningID = jedis.incr(runningKey).toString();
		ArrayList<String> sgHeader = getHeader(sgList);
		ArrayList<String> sgBody = getBody(sgList,sgHeader);

		redisFailed(sgHeader,"Failed");
		redisFailed(sgHeader,"Succeeded");
		redisFailed(sgHeader,"Disabled");

		List<String> keys = jedis.lrange(tempKeys + runningID, 0, -1);

		//System.exit(0);

		String saveSets = jedis.hget("section","Successful Save Sets");
		
		List<String> sectionData = returnList(saveSets);
		
		
		String ss = "";
		ArrayList<String> serverInfo;
		for (String k : keys) {
			System.out.println(k);
			ss = jedis.lpop(k);
			while (ss != null) {

				System.out.println(ss);
				serverInfo = getServerInfo(sectionData,ss);
				sendDetails(serverInfo);
				ss = jedis.lpop(k);

			}
		}


		//String sbody = jedis.hget(sgBodyKey + ss.trim() + runningID, "ch00deu11");

		//System.out.println(sbody);



		//jedis.lpush(tempKeys + runningID, sgBodyKey + runningID);

		//for (String s : sgHeader) {
		//	System.out.println(s);
		//}
		jedis.del("section");
		cleanUpKeys(tempKeys + runningID);
		jedis.disconnect();

	}


	public static void sendDetails(List<String> sectionData) {

		String level = "";
		double files = 0;
		String temp = "";
		long size = 0;
		String sizeTemp = "";
		String server = "";
		String saveset = "";


		for (String s :sectionData) {
			if (s.startsWith("  ")) {
				System.out.println(s);
				if (s.contains("retried") == false && s.contains("aborted, group hard limit has been reached") == false) { 
					level = s.split("level=")[1].split(",")[0];
					temp = s.split(",")[1].trim();
					String[] temp1 = temp.split(" ");

					files = Double.parseDouble(temp1[temp1.length-2]);
					
					server = s.split(":")[0].trim();
					saveset = s.trim().split(" ")[1];
					
					sizeTemp = temp1[0] + " " + temp1[1];
					System.out.println(sizeTemp);
					size = convertToBytes(sizeTemp);
					size = size /1024/1024;

					jedis.zincrby("prod:backup-stats-count:nw:eu#files", files, server);
					jedis.zincrby("prod:backup-stats-count:nw:eu#" + level,1, server);
					jedis.incr("prod:backup-stats-count:nw:eu:total#" + level);
					jedis.incr("prod:backup-stats-count:nw:eu#total");
					jedis.incrBy("prod:backup-stats-count:nw:eu:total#files" , (long) files);
					jedis.incr("prod:backup-stats-count:nw:eu:total#Saveset");
					jedis.zincrby("prod:backup-stats-count:nw:eu#Saveset",1,server);
					jedis.zincrby("prod:backup-stats-count:nw:eu#size",size,server);
					jedis.incrBy("prod:backup-stats-count:nw:eu:total#size",size);


					
					
					//System.out.println("Level :" + level);
					//System.out.println("Files :" + files);
					//System.out.println("Size :" + size);
					//System.out.println("Server :" + server);
					//System.out.println("Saveset :" + saveset);
				}
			}
		}
		//jedis.expire("prod:backup-stats-count:nw:eu#files", 20);
		//jedis.expire("prod:backup-stats-count:nw:eu:total#files", 20);
		//jedis.expire("prod:backup-stats-count:nw:eu:total#full", 20);
		//jedis.expire("prod:backup-stats-count:nw:eu:total#incr", 20);

	}


	public static String getServerInfoStr(ArrayList<String> body,String serverName) {

		String list  = "";

		for (String sLine :body) {
			if (sLine.contains(serverName + ":")) {
				list = list + sLine + "\n";
			}
		}	
		return list;
	}

	public static ArrayList<String> getServerInfo(List<String> sectionData,String serverName) {

		ArrayList<String> list = new ArrayList<String>();



		for (String sLine :sectionData) {
			if (sLine.contains(serverName)) {
				list.add(sLine);
			}
		}	
		return list;
	}

	public static ArrayList<String> getHeader(List<String> sgList) {


		ArrayList<String> list = new ArrayList<String>();

		for (String sLine :sgList) {
			if (sLine.startsWith("---")) {
				break;
			} 
			list.add(sLine);

		}	
		return list;
	}
	public static ArrayList<String> getBody(List<String> sgList, ArrayList<String> sgHeader) {

		int lsize = sgHeader.size();

		for(int i=0; i<lsize ; i++) {
			sgList.remove(0);
		}


		int ii = 0;
		String sec = "";
		String sectionName = "";
		int sint = 0;
		for (String stemp : sgList) {
			//sec = sec + stemp + "\n";
			if (stemp.contains("---")) {
				if (sint == 0) {
					sectionName = stemp.replace("-", "").trim();
					sec = sec + stemp + "\n";
					sint = 1;
				} else if (sint ==1) {
					jedis.hset("section", sectionName, sec);
					sint = 1;
					sectionName = stemp.replace("-", "").trim();
					//sec = "";
					sec = stemp + "\n";
				}
			} else {
				sec = sec + stemp + "\n";
			}

		}
		
		if (sint == 1){
			jedis.hset("section", sectionName, sec);
		}


		/**

		List<String> sec1 = new ArrayList<String>();
		List<String> sec2 = new ArrayList<String>();
		List<String> sec3 = new ArrayList<String>();
		List<String> sec4 = new ArrayList<String>();

		Boolean noMoreData = false;
		String sectionName = "";

		int sNum1 = 0;
		int sNum2 = 0;

		String secNum1 = jedis.lpop("test");
		if (secNum1 != null) {
			sNum1 = Integer.parseInt(secNum1.split(":")[1]);
		}

		String secNum2 = jedis.lpop("test");
		if (secNum2 != null) {
			sNum2 = Integer.parseInt(secNum2.split(":")[1]);
		} else {
			sNum2 = sgList.size();
		}

		sec1 = returnListSection(sgList,sNum1, sNum2);
		String s1s = "";
		for (String sss1 : sec1) {
			s1s = s1s + sss1 + "\n";
		}
		jedis.hset("section", secNum1.split(":")[0], s1s);


		sNum1 = sNum2;
		sNum2 = 0;
		secNum2 = jedis.lpop("test");
		if (secNum2 != null) {
			sNum2 = Integer.parseInt(secNum2.split(":")[1]);
		} else {
			//noMoreData = true;
			sNum2 = sgList.size();
		}
		sec2 = returnListSection(sgList,sNum1, sNum2);
		s1s = "";
		for (String sss1 : sec2) {
			s1s = s1s + sss1 + "\n";
		}
		if (noMoreData == false) {
			jedis.hset("section", secNum2.split(":")[0], s1s);	

			sNum1 = sNum2;
			sNum2 = 0;
			secNum2 = jedis.lpop("test");
			if (secNum2 != null) {
				sNum2 = Integer.parseInt(secNum2.split(":")[1]);
			} else {
				noMoreData = true;
				sNum2 = sgList.size();
			}



			sec3 = returnListSection(sgList,sNum1, sNum2);
		}

		if (noMoreData == false) {
			s1s = "";
			for (String sss1 : sec3) {
				s1s = s1s + sss1 + "\n";
			}
			jedis.hset("section", secNum2.split(":")[0], s1s);	


			sNum1 = sNum2;
			sNum2 = 0;
			secNum2 = jedis.lpop("test");
			if (secNum2 != null) {
				sNum2 = Integer.parseInt(secNum2.split(":")[1]);
			} else {
				sNum2 = sgList.size();
			}
			sec4 = returnListSection(sgList,sNum1, sNum2);
		}

		 **/


		return (ArrayList<String>) sgList;
	}

	public static List<String> returnListSection(ArrayList<String> sgBody, int start, int end) {


		List<String> list = null;
		list = sgBody.subList(start, end);

		return list;
	}


	public static void redisFailed(ArrayList<String> sgHeader, String status) {

		Boolean anyupdate = false;

		for (String sLine : sgHeader) {
			if (sLine.startsWith(status)) {
				String temp1 = sLine.split(":")[1];
				String[] temp2 = temp1.split(",");
				for (String ss : temp2) {
					jedis.rpush(runningKey + status + "#" + runningID, ss.trim());
					jedis.rpush(tempKeys + runningID, runningKey + status + "#" + runningID);
				}
				anyupdate = true;
			}

		}
		//if (anyupdate) jedis.rpush(tempKeys + runningID, runningKey + status + "#" + runningID);
	}

	public static void cleanUpKeys(String runningKey) {
		List<String> keysToDel = jedis.lrange(runningKey, 0, -1);

		for (String kdel : keysToDel) {
			jedis.del(kdel);
		}
		jedis.del(runningKey);
	}

	public static List<String> returnList(String str) {
	
	ArrayList<String> sgList = new ArrayList<String>();

	String[] tempList = str.split("\n");

	for (String s : tempList) {
		sgList.add(s);
	}
	return sgList;
	
	}
	
	public static long convertToBytes(String sunit) {

		if (sunit == null){
			return 0;
		}
		
		long units = 0;
		int n = Integer.parseInt(sunit.split(" ")[0]);

		try {
			sunit = sunit.split(" ")[1];
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return 0;
		}

		sunit = sunit.toUpperCase();

		if (sunit.equals("KB")) units = 1024;
		if (sunit.equals("MB")) units = 1048576;
		if (sunit.equals("GB")) units = 1073741824;
		//if (sunit.equals("TB")) units = 1099511627776;
		return n * units;
	}

	
}
